class Solution:
	# @param words, a list of strings
	# @param L, an integer
	# @return a list of strings
	def fullJustify(self, words, L):
		i = 0
		temp = 0
		strs = []
		currLine = []
		while i < len(words):
			if temp + len(words[i]) <= L:
				currLine.append(words[i])
				temp += len(words[i]) + 1
			else:
				strs.append(currLine)
				temp = len(words[i])+1
				currLine = []
				currLine.append(words[i])
			i += 1
		if len(currLine) > 0:
			strs.append(currLine)
		return self.adjust(strs, L)

	def adjust(self, strs, L):
		ans = []
		for words in strs[:-1]:
			total = 0
			for word in words:
				total += len(word)
			if len(words) > 1:
				q = (L - total) / (len(words)-1)
				r = (L - total) % (len(words)-1)
				line = words[0]
				for i in xrange(1, len(words)):
					line += " " * (q+1 if i <= r else q)
					line += words[i]
			else:
				line = words[0] + " " * (L-len(words[0]))
			ans.append(line)
		last = " ".join(strs[-1])
		last += " " * (L - len(last))
		ans.append(last)
		return ans




if __name__ == '__main__':
	so = Solution()
	print so.fullJustify(["Here","is","an","example","of","text","justification."], 14)